<?php
/* 临时闭馆相关操作控制器 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class TemporaryCloseController extends HomeController
{
    //获取列表
    public function list(Request $request)
    {
        try {
            // 分页参数
            $page = $request->input('page', 1);
            $limit = $request->input('limit', 10);

            //要查询的字段
            $field = ['id', 'start_time', 'end_time', 'memo', 'status'];
            // 查询数据表，默认查询未删除的数据,状态，1=正常，2=删除，3=已闭馆
            $data = DB::table('temporary_close')->where('status', '!=', 2)->select($field)->orderBy('created_at', 'desc')->offset(($page - 1) * $limit)->limit($limit)->get();

            // 统计总记录数
            $count = DB::table('temporary_close')->count();

            return response()->json([
                'code' => HTTP_OK,
                'message' => HTTP_OK_MESSAGE,
                'data' => $data,
                'count' => $count,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取临时闭馆列表失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '获取临时闭馆列表失败',
                'data' => null
            ]);
        }
    }

    //添加
    public function insert(Request $request)
    {
        try {
            //接受参数并组装成数组
            $data = [];
            $data['start_time'] = $request->input('start_time');
            $data['end_time'] = $request->input('end_time');
            $data['memo'] = $request->input('memo');
            $data['add_id'] = $this->user_id;
            if ($data['start_time'] >= $data['end_time']) {
                return response()->json(['code' => HTTP_ERROR,'message' => '开始时间不能大于结束时间']);
            }

            //添加到数据表
            DB::table('temporary_close')->insert($data);

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('添加临时闭馆失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '添加临时闭馆失败',
                'data' => null
            ]);
        }
    }

    //删除
    public function delete(Request $request)
    {
        try {
            //接受参数
            $id = $request->input('id');
            //这里使用逻辑删除，把删除状态改为2
            DB::table('temporary_close')->where('id', $id)->update(['status' => 2]);

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('删除临时闭馆失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '删除临时闭馆失败',
                'data' => null
            ]);
        }
    }


    //修改
    public function update(Request $request)
    {
        try {
            //接受参数
            $id = $request->input('id');

            //接受参数并组装成数组
            $data = [];
            $data['start_time'] = $request->input('start_time');
            $data['end_time'] = $request->input('end_time');
            $data['memo'] = $request->input('memo');

            if ($data['start_time'] >= $data['end_time']) {
                return response()->json(['code' => HTTP_ERROR,'message' => '开始时间不能大于结束时间']);
            }

            //修改数据到数据表
            DB::table('temporary_close')->where('id', $id)->update($data);

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('修改临时闭馆失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '修改临时闭馆失败',
                'data' => null
            ]);
        }
    }

    //详情
    public function detail(Request $request)
    {
        try {
            //接受参数
            $id = $request->input('id');
            $field = ['id', 'start_time', 'end_time', 'memo', 'status'];
            //根据id查询详情
            $data = DB::table('temporary_close')->where('id', $id)->select($field)->first();

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE,'data' => $data]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取临时闭馆详情失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '获取临时闭馆详情失败',
                'data' => null
            ]);
        }
    }

}
